/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!				\file		MdiDraw.cc
				\date		june 2010
				\author		TNick

				\brief		Contains the implementation of MdiDraw class and related


	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	Please read ReadMe.txt and Licence.txt in root folder
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"OGLDraw.h"
#include	"MainWindow.h"
#include	<base/CC_App.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */


/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
MdiDraw::MdiDraw		(QWidget *parent,
						 QString pFile)
/*:QMdiSubWindow(parent)*/
							 :QMdiSubWindow(parent)
{
	/* we'll use this to tell if the class is valid or not */
	pDraw = NULL;
	uFlg = NOFLAGS;
	/* correlation structure is rooted here; this function is, as a result,
	  responsable for showing any resulting error messages */
	CC_Err		lcErr;



	/* splitter */
	QSplitter *spl_med = new QSplitter(this);
	spl_med->setOrientation(Qt::Vertical);

	MainWindow* pMV = qobject_cast<MainWindow *>(parent->parent());
	pName.clear();
	setMinimumSize(100,100);



	// create drawing area
	pDA = new OGLDraw(this);
	spl_med->addWidget(pDA);

	//// create toolbar that allows to change between views
	QToolBar*	seltoolbar = new QToolBar(tr("views"), this);
	//addToolBar(Qt::BottomToolBarArea, seltoolbar);
	seltoolbar->setAllowedAreas(Qt::AllToolBarAreas);
	seltoolbar->setOrientation(Qt::Horizontal);
	seltoolbar->setToolButtonStyle(Qt::ToolButtonTextOnly);
	seltoolbar->setFloatable(false);
	// add model space view
	QAction*	newAct = new QAction(tr("Model"), this);
	// todo newAct->setShortcuts(QKeySequence::New);
	newAct->setStatusTip(tr("Switch to model view"));
	connect(newAct, SIGNAL(triggered()), this, SLOT(switchview_model()));
	seltoolbar->addAction(newAct);

	spl_med->addWidget(seltoolbar);

	spl_med->setStretchFactor(0,1);
	spl_med->setStretchFactor(1,0);
	setWidget(spl_med);

	// we need to create a new draw and to associate it with this widget
	pDraw = new CC_Draw();
	if (!pDraw->InitOk())
		{
			goto Fail_01;
		}
	else
		{
			uFlg |= UNTITLED;
			if (!SUCCEDED(pDraw->performNew(&lcErr)))
				{
				goto Fail_01;
				}
		}

	// generate a name for this drawing
	if (!pFile.isEmpty())
		{ // we have a file; get it's name
			QFileInfo   fi(pFile);
			pName = fi.baseName();
		}
	else
		{ // we have created an empty file; get a proper name
			pName = pMV->GetUniqName();
		}
	setWindowTitle(pName);




	pMV->newMDI(this);
	return;

Fail_01:
	delete pDraw;
	pDraw = NULL;
	lcErr.Add(CCERR::EERR, CCERR::CTX_DRAWINT,
			  TR::tr("New drawing: internal failure"));
	return;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
MdiDraw::~MdiDraw		()
{
	// get rid of associated internal drawing object
	if (pDraw != NULL)
		delete pDraw;
	pName.clear();

}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
bool		MdiDraw::save			()
{
	CC_Err			Err;

	if (isUntitled())
		{
			return saveAs();
		}
	else
		{
			return pDraw->Save(QString(),&Err,CC_Draw::CURRENT);
		}
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		MdiDraw::saveAs			()
{
	CC_Err			Err;
	// let the user choose a location and name
	//  QString fileName = QFileDialog::getSaveFileName(
	//	this, tr("Save As"), pDraw->filename());
	//  if (fileName.isEmpty())
	//return false;

	// as draw class is quiet, we need to display any error message that
	// might have been generated
	//  if (pDraw->saveFile(fileName))
	//  {
	//return true;
	//  }
	//  else
	//  {

	////QMessageBox::warning(parent,tr("Error"),*pDraw->GLE());
	//return false;
	//  }
	return true;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		MdiDraw::switchview_model			(void)
{


}
/* ========================================================================= */


/*  FUNCTIONS    =========================================================== */
//
//
//
//
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
